﻿using Common.Utils;
using Newtonsoft.Json;
using ReactiveUI.Fody.Helpers;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reactive;
using System.Text;
using System.Threading.Tasks;
using Common.ViewModels.调度;
using ReactiveUI;

namespace Common.ViewModels
{
    /// <summary>
    /// 生产线
    /// </summary>
    [JsonObject(MemberSerialization.OptOut)]
    [Serializable]
    public partial class ProductLine : ViewModelBase
    {
        #region 单例

        public static readonly string fileName = $"ProductLine.json";


        private static ProductLine m_Instance;

        public static ProductLine Instance => m_Instance;



        static ProductLine()
        {
            try
            {
                //ProductLine line 
                string json = File.ReadAllText($"{CommonFilePath.ConfigDire}\\{fileName}");
                ProductLine line = JsonConvert.DeserializeObject<ProductLine>(json,
                    new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All, PreserveReferencesHandling = PreserveReferencesHandling.Objects, MaxDepth = 256 });

                //ProductLine line = IOCHelp.GetT<ProductLine>();
                m_Instance = line;
                //m_Instance = CreateSelf();
                //foreach (var item in line.PassVMs)
                //{
                //    item.TempStations.Count;
                //}
            }
            catch (Exception e)
            {
                m_Instance = new ProductLine();
            }
        }

        public static ProductLine CreateSelf()
        {
            try
            {
                string json = File.ReadAllText($"{CommonFilePath.ConfigDire}\\{fileName}");
                ProductLine line = JsonConvert.DeserializeObject<ProductLine>(json,
                    new JsonSerializerSettings
                    {
                        TypeNameHandling = TypeNameHandling.All,
                        PreserveReferencesHandling = PreserveReferencesHandling.Objects,
                        MaxDepth = 256
                    });
                return line;
            }
            catch (Exception e)
            {
                return new ProductLine();
            }


        }


        /// <summary>
        /// 选择的值
        /// </summary>
        [Reactive][JsonIgnore] public object SelectValue { get; set; }

        /// <summary>
        /// 是否手动
        /// </summary>
        [Reactive][JsonIgnore] public bool IsMasual { get; set; } = true;

        [JsonIgnore][Reactive] public bool 重启中 { get; set; } = false;

        [Reactive] public bool 是否显示槽位 { get; set; }=false;

        /// <summary>
        /// 小车真实速度(需要统一,否则计算碰撞会异常麻烦)
        /// </summary>
        [Reactive]
        [JsonProperty]
        public int CarSpeed { get; set; } = 2000;

        /// <summary>
        /// 摆渡(需要统一,否则计算碰撞会异常麻烦)
        /// </summary>
        [Reactive]
        [JsonProperty]
        public int BoatSpeed { get; set; } = 200;

        /// <summary>
        ///
        /// </summary>
        [Reactive]
        [JsonProperty]
        public int 摆渡加速时间 { get; set; } = 0;

        [Reactive]
        [JsonProperty]
        public int 机械手自由时间 { get; set; }

        [Reactive]
        [JsonProperty]
        public double 小池子间隔 { get; set; } = 10;


        /// <summary>
        /// 池子列表
        /// </summary>
        [Reactive]
        [JsonProperty]
        public ObservableCollection<池子> 池子s { get; set; } = new();

        [JsonIgnore]
        public ReactiveCommand<Unit, Unit> CommandCreate工位类型 => ReactiveCommand.Create(() =>
        {
            工位类型s.Add(new 工位类型());
        });

        /// <summary>
        /// 工位类型列表
        /// </summary>
        [Reactive] [JsonProperty] public ObservableCollection<工位类型> 工位类型s { get; set; } = new();

        /// <summary>
        /// 工位信息
        /// </summary>
        [Reactive][JsonProperty] public ObservableCollection<WorkStation> WorkStations { get; set; } = new();


        /// <summary>
        /// 工位信息
        /// </summary>
        [Reactive][JsonProperty] public ObservableCollection<PassVM> PassVMs { get; set; } = new();

        /// <summary>
        /// 工位信息
        /// </summary>
        [Reactive][JsonProperty] public ObservableCollection<Station> Stations { get; set; } = new();

        /// <summary>
        /// 片区列表
        /// </summary>
        [Reactive][JsonProperty] public ObservableCollection<片区> 片区s { get; set; } = new();

        /// <summary>
        /// 片区列表
        /// </summary>
        [Reactive][JsonProperty] public ObservableCollection<Car> Cars { get; set; } = new();

       

        /// <summary>
        /// 默认不再跨片区进行调度
        /// </summary>
        [Reactive][JsonProperty] public bool 是否跨片区调度 { get; set; } = false;

        [Reactive][JsonProperty] public 主通道调度 主通道调度 { get; set; } = new();

        /// <summary>
        /// 机械手提取工件时间 提取工件外加收缩机械手臂(默认3秒)
        /// </summary>
        [Reactive]
        [JsonProperty]
        public int 机械手提取工件时间 { get; set; } = 3000;

        [JsonProperty]
        [Reactive]
        public ObservableCollection<取号叫号系统> RequestEntities { get; set; } = new();

        /// <summary>
        /// 已经保存的配方
        /// </summary>
        [JsonProperty]
        [Reactive]
        public ObservableCollection<配方> 配方s { get; set; }=new ();

        /// <summary>
        /// 已经保存的配方
        /// </summary>
        [JsonIgnore]
        [Reactive]
        public 配方 Select配方 { get; set; }

        #region 平移和缩放

        #endregion
        /// <summary>
        /// 缩放
        /// </summary>
        [Reactive][JsonProperty] public double ScaleX { get; set; } = 0.1;

        /// <summary>
        /// 缩放
        /// </summary>
        [Reactive][JsonProperty] public double ScaleY { get; set; } = 0.04;

        [Reactive][JsonProperty] public double X { get; set; } = 0;

        [Reactive][JsonProperty] public double Y { get; set; } = 0;


        public List<调度任务> Lis调度任务 = new List<调度任务>();

        

        /// <summary>
        /// 调度任务锁
        /// </summary>
        public readonly object m_LockLis调度任务 = new();



        /// <summary>
        /// 调度
        /// </summary>
        public void Deal()
        {
            while (true)
            {
                Thread.Sleep(100);
                try
                {
                    //这里判断调度任务是否冲突

                    foreach (调度任务 调度任务 in Lis调度任务)
                    {

                    }
                }
                catch (Exception e)
                {

                }
            }
        }

        public void Save()
        {
            Save(CommonFilePath.ConfigDire, fileName);
        }


        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="foderPath">文件路径</param>
        /// <param name="filePath">文件路径</param>
        public void Save(string foderPath, string filePath)
        {
            //创建文件夹路径
            if (!Directory.Exists(foderPath))
            {
                Directory.CreateDirectory(foderPath);
            }


            var set = new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.All,
                PreserveReferencesHandling = PreserveReferencesHandling.Objects
            };
            string json = JsonConvert.SerializeObject(this, set);
            File.WriteAllText($"{foderPath}\\{filePath}", json);



        }
        #endregion
    }
}
